package sibu.cn.mydemo.charts.sample1;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

import sibu.cn.mydemo.R;
import sibu.cn.mydemo.charts.library.view.MyLineChartView;

public class MyChartViewActivity extends AppCompatActivity implements View.OnClickListener{

    MyLineChartView myLineChartView ;
    Button button;
    float topY = 17.9f;
    float centerY = 16.0f;
    float bottomY = 14.5f;
    float change = 15.0f;
    float dataY[][] = new float[2][3];
    Calendar calendar = Calendar.getInstance();
    int day;
    int hour;
    int minute;
    int second;
    long time=0;
    Timer timer = new Timer(true);
    TimerTask timerTask = new TimerTask() {
        @Override
        public void run() {
            dealWithTime();
            myLineChartView.changeXY(change,time);
            myLineChartView.update();

            if(change<17){
                change++;
            }else {
                change--;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_chart_view);
        init();
        addListener();
    }

    public void  init(){
        myLineChartView = (MyLineChartView) findViewById(R.id.myLineChartView);
        button  = (Button) findViewById(R.id.my_chart_data);

    }

    public void dealWithTime(){
        calendar.setTimeInMillis(System.currentTimeMillis());
        day = calendar.get(Calendar.DAY_OF_YEAR);
        hour = calendar.get(Calendar.HOUR_OF_DAY);
        minute = calendar.get(Calendar.MINUTE);
        second = calendar.get(Calendar.SECOND);
        Log.e("calendar", "day = " + day + " hour = " + hour + " minute = " + minute + " second = " + second);

       /* if(hour==9&&minute>30){
           time =(minute-30)*60+second;
        }
        if(hour>9&&hour<11){
           time =(hour-9)*60*60+minute*60+second;
        }
        if(hour==11&&minute>30){
            time = (minute-30)*60+second;
        }
        if(hour>13&&hour<15){
            time = (hour-13)*60*60+minute*60+second;
        }*/
        time = 4*60*20;
        Log.e("dealWithTime","dealWithTime = "+time);
    }

    public void addListener(){
        button.setOnClickListener(this);
    }

    public static void jumpMyChartViewActivity(Context context){
        Intent intent = new Intent(context,MyChartViewActivity.class);
        context.startActivity(intent);
    }

    @Override
    public void onClick(View v) {
        dataY[0][0] =topY;
        dataY[0][1] =centerY;
        dataY[0][2] = bottomY;
        myLineChartView.setCoordinates(dataY);

        timer.schedule(timerTask,1000,3000);

    }
}
